<template>

<div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="roleTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="会员描述" align="center">
          <template slot-scope="scope">{{scope.row.des}}</template>
        </el-table-column>  
       
        <el-table-column label="金额" align="center">
          <template slot-scope="scope">{{scope.row.amount}}</template>
        </el-table-column>  
       
        <el-table-column label="佣金" align="center">
          <template slot-scope="scope">{{scope.row.commission}}</template>
        </el-table-column>  
        
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope"> 
            <el-row>
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>

            <el-button size="mini"
                       type="text"
                       @click="handleRelate(scope.row)">
              关联权益
            </el-button>

<!-- 
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button> -->
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form ref="form" label-width="80px" style="width: 400px;">

      <el-form-item label="会员描述">
        <el-input v-model="formData.des"></el-input>
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="formData.amount" type="number"></el-input>
      </el-form-item>
      <el-form-item label="佣金">
        <el-input v-model="formData.commission" type="number"></el-input>
      </el-form-item> 
      <el-form-item label="支付方式">
        <el-select v-model="formData.online" placeholder="请选择"> 
          <el-option label="线下" :value="0"></el-option>
          <el-option label="线上" :value="1"></el-option>
        </el-select>
      </el-form-item>  
      <el-form-item label="图标：">
          <single-upload v-model="formData.icon"></single-upload>
        </el-form-item> 
    </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="isEdit?'编辑分类':'添加分类'"
      :visible.sync="relateDialogVisible"
      width="40%">


    <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选

    </el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
          <el-checkbox v-for="city in cities" :value="city.id" :label="city.id" :key="city.id">{{city.title}}</el-checkbox>
        </el-checkbox-group>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="relateDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogRelateConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>



 
</template>

<script>
import { list,add,edit,del } from '@/api/vipConfig'; 
import {list as relateList,umsVipRightRelation} from '@/api/umsVipRights';
  import SingleUpload from '@/components/Upload/singleUpload'
const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    keyword: null
  };
  export default { 
    components: {SingleUpload},
    data() {
      return {
        checkAll: false,
        checkedCities: [],
        cities: [],
        isIndeterminate: false,
        cityOptions:[],
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        vipId:'',
        relateDialogVisible:false,
        listLoading: false,
        dialogVisible: false,
        formData:{
          des:'',
          online:1,
          icon:'',
          amount:0,
          createTime:'',
          commission:0
        },
        isEdit: false
      };
    },
    created() {
      //this.getDetail()
      this.getList();
    },
    methods: {
      handleDialogRelateConfirm(){
        umsVipRightRelation({
          vipId:this.vipId,
          rightIds:this.checkedCities
        }).then(response => {
          this.$message({
            message: '关联成功！',
            type: 'success'
          });
          this.relateDialogVisible = false;
        })
      },
      handleRelate(row){ 
        this.vipId = row.id;
        this.checkedCities = row.rights.map(item => item.id)
        relateList({pageNum:1,pageSize:99,}).then(response => {
          this.cities = response.data.list
          this.cityOptions = response.data.list.map(item => item.id)
          this.checkAll = this.checkedCities.length === this.cityOptions.length;
        });
        this.relateDialogVisible = true;
      },
      handleCheckAllChange(val) { 
        this.checkedCities = val ? this.cityOptions : [];
        this.isIndeterminate = false;
      },
      handleCheckedCitiesChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.cities.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true; 
        for (const key in this.formData) {
            if (row[key] != null && row[key] != undefined) { 
              this.formData[key] = row[key]
            }
        } 
        this.formData.id = row.id; 
      },

      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            edit(this.formData).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            add(this.formData).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      }, 

      handleDelete(index, row) {
        this.$confirm('是否要删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let id = [];
          id.push(row.id);
          let params=new URLSearchParams();
          params.append("id",id);
          del(params).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.formData = Object.assign({},defaultData);
      },
    
      handleSubmit(){
        edit(this.formData).then(response => {
          this.$message({
            message: '添加成功！',
            type: 'success'
          }); 
          this.getDetail();
        })
      },  
      getList() {
        this.listLoading = true;
        list(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  };
</script>

<style scoped></style>